# -*- coding: utf-8 -*-

"""
 (c) 2023 - Copyright CTyunOS Inc

 Authors:
   youyifeng <youyf2@chinatelecom.cn>

"""

from .base import DBModel
from sqlalchemy import Column
from sqlalchemy import Text
from sqlalchemy import Integer


class CVE(DBModel):
    __tablename__ = 'OpenEulerCVE'
    id = Column('id', Integer, primary_key=True)
    affectedProduct = Column('affectedProduct', Text)
    announcementTime = Column('announcementTime', Text)
    attackComplexityNVD = Column('attackComplexityNVD', Text)
    attackComplexityOE = Column('attackComplexityOE', Text)
    attackVectorNVD = Column('attackVectorNVD', Text)
    attackVectorOE = Column('attackVectorOE', Text)
    availabilityNVD = Column('availabilityNVD', Text)
    availabilityOE = Column('availabilityOE', Text)
    confidentialityNVD = Column('confidentialityNVD', Text)
    confidentialityOE = Column('confidentialityOE', Text)
    cveId = Column('cveId', Text)
    cvrf = Column('cvrf', Text)
    cvsssCoreNVD = Column('cvsssCoreNVD', Text)
    cvsssCoreOE = Column('cvsssCoreOE', Text)
    integrityNVD = Column('integrityNVD', Text)
    integrityOE = Column('integrityOE', Text)
    nationalCyberAwarenessSystem = Column('nationalCyberAwarenessSystem', Text)
    packageList = Column('packageList', Text)
    parserBean = Column('parserBean', Text)
    privilegesRequiredNVD = Column('privilegesRequiredNVD', Text)
    privilegesRequiredOE = Column('privilegesRequiredOE', Text)
    scopeNVD = Column('scopeNVD', Text)
    scopeOE = Column('scopeOE', Text)
    securityNoticeNo = Column('securityNoticeNo', Text)
    status = Column('status', Text)
    summary = Column('summary', Text)
    type = Column('type', Text)
    updateTime = Column('updateTime', Text)
    userInteractionNVD = Column('userInteractionNVD', Text)
    userInteractionOE = Column('userInteractionOE', Text)
    packageName = Column('packageName', Text)

    COLUMN = [
        "id",
        "affectedProduct",
        "announcementTime",
        "attackComplexityNVD",
        "attackComplexityOE",
        "attackVectorNVD",
        "attackVectorOE",
        "availabilityNVD",
        "availabilityOE",
        "confidentialityNVD",
        "confidentialityOE",
        "cveId",
        "cvsssCoreNVD",
        "cvsssCoreOE",
        "integrityNVD",
        "integrityOE",
        "nationalCyberAwarenessSystem",
        "packageName",
        "privilegesRequiredNVD",
        "privilegesRequiredOE",
        "scopeNVD",
        "scopeOE",
        "status",
        "summary",
        "type",
        "userInteractionNVD",
        "userInteractionOE",
        "createTime",
        "updateTime",
        "securityNoticeNo",
        "parserBean",
        "cvrf",
        "packageList",
    ]

    @staticmethod
    def pretty_output_title():
        print("%-15s %-30s %-10s %-20s" % ("CVEID", "包名", "评分", "发布时间"))

    def pretty_output(self):
        print("%-15s %-30s %-10s %-20s" % (
            self.cveId,
            self.packageName.replace('\n',' '),
            self.cvsssCoreNVD if self.cvsssCoreNVD else self.cvsssCoreOE,
            self.updateTime,
        ))

    def simple_output(self):
        print("""CVEID: %s
评分:%s 
包名: %s
发布时间: %s
详情链接: https://www.openeuler.org/zh/security/cve/detail/?cveId=%s&packageName=%s
""" % (
            self.cveId,
            self.cvsssCoreNVD if self.cvsssCoreNVD else self.cvsssCoreOE,
            self.packageName,
            self.updateTime,
            self.cveId,
            self.packageName,
        ))
